package com.hxzy.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.hxzy.bean.User;
import com.hxzy.bean.UserState;
import com.hxzy.service.UserService;
import com.hxzy.service.impl.UserServiceImpl;
import com.hxzy.vo.JsonResult;
import com.hxzy.vo.StatusCode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author nick
 */
@WebServlet("/doLogin")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        //设置响应的类型为json
        resp.setContentType("application/json;charset=utf-8");//告诉浏览器响应的数据为json格式

        //获取用户名和密码
        String userName = req.getParameter("userName");
        String userPwd = req.getParameter("userPwd");

        //用户名和密码非空验证
        if (Objects.nonNull(userName) && Objects.nonNull(userPwd)) {
            UserService service = new UserServiceImpl();
            //1.向数据查询，根据用户名查询用户信息
            User user = service.findByAccount(userName);
            if (user == null) { //没有这个用户
                JsonResult jsonResult = new JsonResult();
                jsonResult.setCode(StatusCode.INVALID.getCode());
                jsonResult.setMessage(StatusCode.INVALID.getMessage());
                write2client(jsonResult, resp);
                return;
            }

            //2.验证密码是否正确
            if (!userPwd.equals(user.getPassword())) { //如果密码不正确
                //响应用户名或密码错误的信息
                JsonResult jsonResult = new JsonResult();
                jsonResult.setCode(StatusCode.INVALID.getCode());
                jsonResult.setMessage(StatusCode.INVALID.getMessage());
                write2client(jsonResult, resp);
                return;
            }

            //3.验证用户的状态，如果为正常则说明具有该用户登录成功
            if (user.getState() != UserState.NORMAL.getState()) { //用户状态不正常
                //响应用户名状态异常
                JsonResult result = new JsonResult();
                result.setCode(StatusCode.LOCKED.getCode());
                result.setMessage(StatusCode.LOCKED.getMessage());

                write2client(result, resp);
                return;

            }

            //4.将登陆的用户信息添加到session作用域
            HttpSession session = req.getSession();
            session.setAttribute("user",user);

            JsonResult result = new JsonResult();
            result.setCode(StatusCode.SUCCESS.getCode());
            result.setMessage(StatusCode.SUCCESS.getMessage());

            Map map = new HashMap();
            map.put("url", "index.jsp"); //设置登录成功后跳转的url地址
            result.setData(map);

            write2client(result, resp);
        } else {
            //响应客户端，返回json格式的数据，告诉客户端本次结果
            JsonResult result = new JsonResult();
            result.setCode(StatusCode.PARAMS_ERROR.getCode());
            result.setMessage(StatusCode.PARAMS_ERROR.getMessage());
            write2client(result,resp);
        }
    }

    private void write2client(JsonResult jsonResult,HttpServletResponse resp) throws IOException {
        //将result转换为json
        String json = JSONObject.toJSONString(jsonResult);
        resp.getWriter().write(json);
        resp.getWriter().close();
    }
}